__author__="Eagle"
__date__ ="$12.4.2011 20:05:37$"

import wx
import wx.richtext
import GUI.gui

class Wysiwyg():
    # @type richtext wx.richtext.RichTextCtrl
    richtext = None

    # @type btnBold wx.ToolBar
    toolbar = None

    def __init__(self, richtext, toolbar):
        self.richtext = richtext
        self.toolbar = toolbar

        self.toolbar.Bind(wx.EVT_TOOL, self.onBold, self.toolbar.FindById(GUI.gui.ID_TOOL_BOLD))
        self.toolbar.Bind(wx.EVT_TOOL, self.onItalic, self.toolbar.FindById(GUI.gui.ID_TOOL_ITALIC))
        self.toolbar.Bind(wx.EVT_TOOL, self.onUnderline,  self.toolbar.FindById(GUI.gui.ID_TOOL_UNDERLINE))

        self.toolbar.Bind(wx.EVT_UPDATE_UI, self.onBoldUpdate, self.toolbar.FindById(GUI.gui.ID_TOOL_BOLD))
        self.toolbar.Bind(wx.EVT_UPDATE_UI, self.onItalicUpdate, self.toolbar.FindById(GUI.gui.ID_TOOL_ITALIC))
        self.toolbar.Bind(wx.EVT_UPDATE_UI, self.onUnderlineUpdate,  self.toolbar.FindById(GUI.gui.ID_TOOL_UNDERLINE))

        idBold = wx.NewId()
        idItalic = wx.NewId()
        idUnderline = wx.NewId()
        richtext.GetParent().Bind(wx.EVT_MENU, self.onBold, id=idBold)
        richtext.GetParent().Bind(wx.EVT_MENU, self.onItalic, id=idItalic)
        richtext.GetParent().Bind(wx.EVT_MENU, self.onUnderline, id=idUnderline)
        accel = [
                    (wx.ACCEL_CTRL, ord('B'), idBold),
                    (wx.ACCEL_CTRL, ord('I'), idItalic),
                    (wx.ACCEL_CTRL, ord('U'), idUnderline)
                ]

        richtext.GetParent().SetAcceleratorTable(wx.AcceleratorTable(accel))

    def onBold(self, event):
        self.richtext.ApplyBoldToSelection()

    def onItalic(self, event):
        self.richtext.ApplyItalicToSelection()

    def onUnderline(self, event):
        self.richtext.ApplyUnderlineToSelection()

    def onBoldUpdate(self, event):
        event.Check(self.richtext.IsSelectionBold())

    def onItalicUpdate(self, event):
        event.Check(self.richtext.IsSelectionItalics())

    def onUnderlineUpdate(self, event):
        event.Check(self.richtext.IsSelectionUnderlined())
